package com.rst.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.MetaDataDTO;
import com.rst.model.TenantConfigMappingDTO;
import com.rst.model.TenantDTO;
import com.rst.service.TenantConfigService;

@Controller
public class TenantConfigController {
	
	private Logger logger = Logger.getLogger(TenantConfigController.class);
	
	@Autowired
	TenantConfigService configService;

	@RequestMapping(value="/addTenant", method=RequestMethod.GET)
	public ModelAndView addTenant()
	{
		logger.info("start addTenant");
		ModelAndView mav = new ModelAndView("tenant/addTenant");
		List<MetaDataDTO> metaDataDTOs = configService.getMataDataList();
		mav.addObject("metaDataDTOs", metaDataDTOs);
		logger.info("end addTenant");	
		return mav;
	}

	@RequestMapping(value="/saveTenant", method=RequestMethod.POST)
	public String saveTenant(SessionStatus status,HttpServletRequest request)
	{		
		Long response = configService.saveTenant(request);
		if(response != null ){
			if(response != null && response.equals(0l)){
				return "redirect:addTenant.htm?res=0";	
			}
			status.setComplete();
			return "redirect:tenantProvision.htm?resId="+response;			
		}
		// we need to send on the error page..
		return null;
	}

	@RequestMapping(value="/tenantProvision", method=RequestMethod.GET)
	public ModelAndView tenantProvision(@RequestParam("resId")Long tenantId)
	{
		logger.info("start tenantProvision");
		ModelAndView mav = new ModelAndView("tenant/provision");
		
		List<MetaDataDTO> metaDataDTOs = configService.getMataDataList();
		mav.addObject("metaDataDTOs", metaDataDTOs);
		
		try {
			if(tenantId != null && tenantId > 0){
				TenantDTO tenant = configService.getTenanatById(tenantId);
				mav.addObject("tenant", tenant);
				logger.info("end tenantProvision");	
				return mav;
			}
			
		} catch (Exception e) {
			logger.info("Exception at tenantId=: "+tenantId);	
			e.printStackTrace();
			// error page 
			return null;	
		}
		logger.info("end tenantProvision");	
		return mav;
	}	
	
	
	@RequestMapping(value="/processTenantProvision", method=RequestMethod.POST)
	public String processTenantProvision(HttpServletRequest request)
	{		
		Long id = configService.processTenantProvision(request);
		if(id != null){
			return "redirect:authenticateSuccess.htm?res=1";	
		}
		return null;
	}

	@RequestMapping(value="/addMetaData", method=RequestMethod.GET)
	public ModelAndView addMetaData()
	{
		logger.info("start addMetaData");
		ModelAndView mav = new ModelAndView("tenant/metaData");
		MetaDataDTO metaData = new MetaDataDTO();		
		mav.getModelMap().put("metaData", metaData);
		logger.info("end addMetaData");	
		return mav;
	}

	@RequestMapping(value="/saveMetaData", method=RequestMethod.POST)
	public String saveMetaData(@ModelAttribute("metaData")MetaDataDTO metaData , SessionStatus status)
	{		
		Long id = configService.saveMetaData(metaData);
		status.setComplete();
		return "redirect:authenticateSuccess.htm?res=1";
	}

	@RequestMapping(value="/xmlConfiguration", method=RequestMethod.GET)
	public  ModelAndView getXml(@RequestParam("tenantId")String tenantId)
	{		
		logger.info("start configuration");
		ModelAndView mav = new ModelAndView("tenant/generateXml");
		TenantConfigMappingDTO config = configService.getXmlByTenantId(tenantId);
		mav.addObject("config", config);
		logger.info("end configuration");
		return mav;
	}
	
	
	@RequestMapping(value="/tenantList", method=RequestMethod.GET)
	public  ModelAndView getTenantList()
	{		

		logger.info("start tenantProvision");
		ModelAndView mav = new ModelAndView("tenant/tenantList");
		List<TenantDTO> tenantList = configService.getTenantList();
		mav.addObject("tenantList", tenantList);
		logger.info("end tenantProvision");
		return mav;
	}

}